﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AAA.Algorithm.Data
{
    public class Neuron : INeuron
    {
        private object _oKey;
        private object _oCluster;
        private Dictionary<string, float> _dicFactor;
        private List<string> _lstFactorName;
        private Dictionary<string, object> _dicAttribute;
        private List<string> _lstAttributeName;

        public Neuron()
        {
            try
            {
                _dicFactor = new Dictionary<string, float>();
                _lstFactorName = new List<string>();
                _dicAttribute = new Dictionary<string, object>();
                _lstAttributeName = new List<string>();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
        }

        #region INeuron Members

        public object Key
        {
            get { return _oKey; }
            set { _oKey = value; }
        }

        public object Cluster
        {
            get { return _oCluster; }
            set { _oCluster = value; }
        }

        public float GetFactor(int iIndex)
        {
            try
            {
                return GetFactor(_lstFactorName[iIndex]);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
            return float.NaN;
        }

        public float GetFactor(string strName)
        {
            try
            {
                return _dicFactor.ContainsKey(strName)
                        ? _dicFactor[strName]
                        : float.NaN;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
            return float.NaN;
        }

        public void SetFactor(int iIndex, float fValue)
        {
            if((iIndex < 0) || (iIndex >= _lstFactorName.Count))
                return;

            SetFactor(_lstFactorName[iIndex], fValue);
        }

        public void SetFactor(string strName, float fValue)
        {
            try
            {
                if (_lstFactorName.IndexOf(strName) < 0)
                    _lstFactorName.Add(strName);

                if(_dicFactor.ContainsKey(strName))
                    _dicFactor[strName] = fValue;
                else
                    _dicFactor.Add(strName, fValue);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
        }

        public int FactorCount
        {
            get { return _lstFactorName.Count; }
        }

        public object GetAttribute(int iIndex)
        {
            try
            {
                return GetAttribute(_lstAttributeName[iIndex]);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
            return null;
        }

        public object GetAttribute(string strName)
        {
            try
            {
                return _dicAttribute.ContainsKey(strName)
                        ? _dicAttribute[strName]
                        : null;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
            return null;
        }

        public void SetAttribute(int iIndex, object oValue)
        {
            if ((iIndex < 0) || (iIndex >= _lstAttributeName.Count))
                return;

            SetAttribute(_lstAttributeName[iIndex], oValue);
        }

        public void SetAttribute(string strName, object oValue)
        {
            try
            {
                if (_lstAttributeName.IndexOf(strName) < 0)
                    _lstAttributeName.Add(strName);

                if (_dicAttribute.ContainsKey(strName))
                    _dicAttribute[strName] = oValue;
                else
                    _dicAttribute.Add(strName, oValue);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
        }

        public int AttributeCount
        {
            get { return _lstAttributeName.Count; }
        }

        public List<string> GetFactorNames
        {
            get { return _lstFactorName; }
        }

        public List<string> GetAttributeNames
        {
            get { return _lstAttributeName; }
        }

        #endregion
    }
}
